﻿
Partial Class WebForm_ManageRegion_AddRegion
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound

    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand
        If e.CommandName = "btnSave" Then
            If Me.SaveData() = True Then
                RaiseEvent Saved(Me, New EventArgs)
            End If
        End If
    End Sub
End Class

Partial Class WebForm_ManageRegion_AddRegion
    Public Sub InitData()
        Me.fv.ChangeMode(FormViewMode.Insert)
        TryCast(fv.FindControl("txtParent"), TextBox).Text = Me.DefaultParentID
        TryCast(fv.FindControl("txtRegionID"), TextBox).Focus()
    End Sub

    Public Sub InitData(ByVal regionID As String)
        Me.fv.ChangeMode(FormViewMode.Edit)
        Me.RegionID = regionID
        Me.BindData()
        TryCast(fv.FindControl("txtRegionID"), TextBox).Enabled = False
        TryCast(fv.FindControl("txtParent"), TextBox).Enabled = False
    End Sub

    Private Sub BindData()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select r.* ")
        sql.AppendFormat(" from Region r ")
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat("and r.RegionID = '{0}' ", Me.RegionID)

        dt = dat.GetData(sql)

        Me.fv.DataSource = dt
        Me.fv.DataBind()
    End Sub


    Private Function SaveData() As Boolean
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Using scope As New Transactions.TransactionScope
            If Me.fv.CurrentMode = FormViewMode.Insert Then
                sql.Length = 0
                sql.AppendFormat("insert Region (RegionID,RegionName,Parent,RegionType) select ")
                sql.AppendFormat(" '{0}' ", TryCast(fv.FindControl("txtRegionID"), TextBox).Text)
                sql.AppendFormat(",N'{0}' ", TryCast(fv.FindControl("txtRegionName"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", TryCast(fv.FindControl("txtParent"), TextBox).Text)
                sql.AppendFormat(",'{0}' ", "V")

                dat.AccData(sql)
                Me.RegionID = TryCast(fv.FindControl("txtRegionID"), TextBox).Text
            ElseIf Me.fv.CurrentMode = FormViewMode.Edit Then
                sql.Length = 0
                sql.AppendFormat("update Region set ")
                sql.AppendFormat(" RegionName=N'{0}' ", TryCast(fv.FindControl("txtRegionName"), TextBox).Text)
                sql.AppendFormat("where RegionID = '{0}' ", TryCast(fv.FindControl("txtRegionID"), TextBox).Text)

                dat.AccData(sql)
            End If

            scope.Complete()
        End Using

        Return True
    End Function

    Public Property RegionID() As String
        Get
            Return ViewState("SelectedRegionID")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedRegionID") = value
        End Set
    End Property

    Public Property DefaultParentID() As String
        Get
            Return ViewState("DefaultParentID")
        End Get
        Set(ByVal value As String)
            ViewState("DefaultParentID") = value
        End Set
    End Property

    Public Event Saved(ByVal source As Object, ByVal e As EventArgs)
End Class